#light

module CGraph.DirectedGraph.Algorithms
open CGraph.DirectedGraph
open CGraph.DirectedGraph.Impl
open CGraph.Generic.Algorithms
open System.Collections.Generic

/// Returns a dictionary of vertex mapped to 
/// depths.  You pass in the initial list of vertexes
/// to start the depth labelling from.  This is safe to call
/// on cyclic graphs (if you didn't need that then a simple depth
/// first traversal would do just fine.
let DepthLabel vertexes =
    let allVertexes = Dictionary<IVertex,int>()
    let traverseDepth = ref 0
    
    let vertexFilter vertex =
        let success,depth = allVertexes.TryGetValue vertex
        allVertexes.[vertex] <- !traverseDepth
        not success
    
    let traverser verts =
        let newVertex = new List<IVertex seq>()
        traverseDepth := !traverseDepth + 1
        for vert in verts |> Seq.filter vertexFilter do
            newVertex.Add (vert.OutgoingEdges |> Seq.map (fun (edge) -> edge.OutgoingVertex))
        if (newVertex.Count) = 0 then None else Some (Seq.concat newVertex)
        
    RecursiveIterate traverser vertexes
    
    allVertexes
    
let RemoveExistingEdge (pred:IEdge-> Edge option) edges =
    let edge = edges |> Seq.first pred
    match edge with
        | Some edge -> 
            let graph = (edge :> IDirectedGraphObject).Graph
            (graph :?> DirectedGraph).RemoveEdge(edge)
        | None -> ()
    